package com.school.vaccination.controller;

import com.school.support.base.BaseController;
import com.school.vaccination.domain.Doctor;
import com.school.vaccination.service.DoctorService;
import com.school.vaccination.service.VaccineService;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.exception.TipException;
import zhibi.common.util.StringUtil;
import zhibi.common.util.security.SecurityUtil;

import java.util.Date;

/**
 * Created by 执笔 on 2017/3/18.
 */
@RequestMapping
@Controller
public class IndexController extends BaseController {

    @Autowired
    private DoctorService doctorService;
    @Autowired
    private VaccineService vaccineService;

    /**
     * 登录后的首页
     * @return
     */
    @RequestMapping({"","index"})
    public String index(Model model){
        Integer total = vaccineService.updateStatusByDate();
        model.addAttribute("total",total);
        return "index";
    }

    @RequestMapping(value = "register",method = RequestMethod.GET)
    public String register(){
        return "register";
    }

    @RequestMapping(value = "register",method = RequestMethod.POST)
    public String register(String username,String password,String card){
        Doctor doctor = new Doctor();
        doctor.setCard(card);
        doctor = doctorService.selectOne(doctor);
        if(null == doctor){
            throw new TipException("该医生证不存在","/register");
        }
        if(!StringUtil.isEmpty(doctor.getUsername())){
            throw new TipException("该医生证已经注册过","/register");
        }
        Doctor dtemp = new Doctor();
        dtemp.setUsername(username);
        dtemp = doctorService.selectOne(dtemp);
        if(null != dtemp){
            throw new TipException("该用户名已经注册过","/register");
        }
        doctor.setAddtime(new Date());
        doctor.setPassword(SecurityUtil.MD5(password,PASSWORD_COUNT));
        doctor.setUsername(username);
        doctorService.updateByIdSelective(doctor);
        session.setAttribute(SESSION_USER,doctor);
        return redirect("/");
    }

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username,String password){
        Doctor doctor = new Doctor();
        doctor.setUsername(username);
        doctor.setPassword(SecurityUtil.MD5(password,PASSWORD_COUNT));
        doctor = doctorService.selectOne(doctor);
        if(null == doctor){
            throw new TipException("用户名或者密码错误","/login");
        }
        session.setAttribute(SESSION_USER,doctor);
        return redirect("/");
    }

    @RequestMapping("logout")
    public String logout(){
        session.removeAttribute(SESSION_USER);
        return "login";
    }
}
